<?php
//   +----------------------------------------------------------------------
//   | Copyright (c) 2015-2025 http://www.hdphp.cn All rights reserved.
//   | Licensed ( http://www.hdphp.cn/licenses/ )
//   | Author: Jack <sophia2152@qq.com>
//   | 官方网址: http://www.hdphp.cn
//   | 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用。
//   | 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
//   +----------------------------------------------------------------------
namespace hdphp\services\wechat\config;

use hdphp\services\wechat\contract\ConfigHandlerInterface;
use hdphp\services\wechat\contract\WorkAppConfigHandlerInterface;
use hdphp\services\wechat\DefaultConfig;

/**
 * Class：企业微信配置
 * 创建人：Jack
 * 创建时间：2023/12/11 10:58
 * @package hdphp\services\wechat\config
 */
class WorkConfig implements ConfigHandlerInterface
{
    // 应用
    const TYPE_APP = 'app';
    // 客户联系
    const TYPE_USER = 'user';
    // 通讯录同步
    const TYPE_ADDRESS = 'address';
    // 客服
    const TYPE_KEFU = 'kefu';
    // 审批
    const TYPE_APPROVE = 'approve';
    // 会议室
    const TYPE_MEETING = 'meeting';
    // 自建应用
    const TYPE_USER_APP = 'build';

    /**
     * @var string
     */
    protected $corpId;

    /**
     * @var string
     */
    protected $token;

    /**
     * @var string
     */
    protected $aesKey;

    /**
     * @var string
     */
    protected $responseType = 'array';

    /**
     * @var LogCommonConfig
     */
    protected $logConfig;

    /**
     * @var HttpCommonConfig
     */
    protected $httpConfig;

    /**
     * @var bool
     */
    protected $init = false;

    /**
     * @var WorkAppConfigHandlerInterface
     */
    protected $handler;

    /**
     * @var array
     */
    protected $appConfig;

    /**
     * WorkConfig constructor.
     * @param LogCommonConfig  $config
     * @param HttpCommonConfig $commonConfig
     */
    public function __construct(LogCommonConfig $config, HttpCommonConfig $commonConfig)
    {
        $this->logConfig = $config;
        $this->httpConfig = $commonConfig;
    }

    protected function init()
    {
        if ($this->init) {
            return;
        }
        $this->init = true;
        $this->corpId = $this->corpId ?: $this->httpConfig->getConfig(DefaultConfig::WORK_CORP_ID, '');
        $this->token = $this->token ?: $this->httpConfig->getConfig('work.token', '');
        $this->aesKey = $this->aesKey ?: $this->httpConfig->getConfig('work.key', '');
    }

    /**
     * 注释：设置
     * 创建人：Jack
     * 创建时间：2023/12/11 10:59
     * @param string $key
     * @param        $value
     * @return $this|mixed
     */
    public function set(string $key, $value)
    {
        $this->{$key} = $value;
        return $this;
    }

    /**
     * 获取配置
     * @param string|null $key
     * @return array|mixed|null
     */
    public function get(string $key = null)
    {
        $this->init();
        if ($key) {
            if (isset($this->{$key})) {
                return $this->{$key};
            }
            if ('log' === $key) {
                return $this->logConfig->all();
            }
            if ('http' === $key) {
                return $this->httpConfig->all();
            }
        }
        return null;
    }

    /**
     * 获取全部值
     * @return array
     */
    public function all(): array
    {
        $this->init();
        return [
            'corp_id' => $this->corpId,
            'token' => $this->token,
            'aes_key' => $this->aesKey,
            'response_type' => $this->responseType,
            'log' => $this->logConfig->all(),
            'http' => $this->httpConfig->all()
        ];
    }

    /**
     * 获取应用配置
     * @param string $type
     * @return array
     */
    public function getAppConfig(string $type): array
    {
        if (!isset($this->appConfig[$type])) {
            /** @var WorkAppConfigHandlerInterface $make */
            $make = app()->make($this->handler);
            if (!$this->corpId) {
                $this->init();
            }
            $this->appConfig[$type] = $make->getAppConfig($this->corpId, $type);
        }
        return $this->appConfig[$type];
    }

    /**
     * 设置
     * @param string $handler
     * @return $this
     */
    public function setHandler(string $handler)
    {
        $this->handler = $handler;
        return $this;
    }
}
